package MIU.TwistedLabirynth.menu;

import java.util.ArrayList;
import java.util.List;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import MIU.TwistedLabirynth.pac.GameLogicController;

public class LevelMenu {

	private final GameLogicController gameLogicController;
	private final List<TextureRegion> levelTextureRegions;

	// LEVEL CHOOSE MENU BUTTONS
	private BitmapTextureAtlas mLevelChooseBackTexture;
	private TextureRegion mLevelChooseBackTextureRegion;

	private BitmapTextureAtlas mLevelChooseMenuTexture;
	private TextureRegion mLevelChooseMenuTextureRegion;

	private BitmapTextureAtlas mLevelChooseNextTexture;
	private TextureRegion mLevelChooseNextTextureRegion;

	private BitmapTextureAtlas mLevelButtonDisabled;
	private TextureRegion mLevelButtonNextDisabledRegion;
	private TextureRegion mLevelButtonBackDisabledRegion;

	// LEVEL number
	private BitmapTextureAtlas mLevelTexture1;

	private TextureRegion mLevelTextureRegionLocked;

	private int currentPage;

	public LevelMenu(GameLogicController gameLogicController) {
		this.gameLogicController = gameLogicController;
		levelTextureRegions = new ArrayList<TextureRegion>(10);
		currentPage = 1;
	}

	public void loadResources() {
		// LEVEL MENU BUTTONS
		mLevelChooseBackTexture = new BitmapTextureAtlas(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mLevelChooseBackTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.mLevelChooseBackTexture, gameLogicController, "menuLevel_black_back.png", 0, 0);

		mLevelChooseMenuTexture = new BitmapTextureAtlas(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mLevelChooseMenuTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.mLevelChooseMenuTexture, gameLogicController, "menuLevel_black_up_.png", 0, 0);

		mLevelChooseNextTexture = new BitmapTextureAtlas(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mLevelChooseNextTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.mLevelChooseNextTexture, gameLogicController, "menuLevel_next.png", 0, 0);

		mLevelButtonDisabled = new BitmapTextureAtlas(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mLevelButtonBackDisabledRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.mLevelButtonDisabled, gameLogicController, "menuLevel_black_back_disabled.png", 0, 0);
		mLevelButtonNextDisabledRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.mLevelButtonDisabled, gameLogicController, "menuLevel_next_disabled.png", 0, 64);

		mLevelTexture1 = new BitmapTextureAtlas(64, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		levelTextureRegions.add(BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mLevelTexture1,
				gameLogicController, "level1.png", 0, 0));
		levelTextureRegions.add(BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mLevelTexture1,
				gameLogicController, "level2.png", 0, 64));
		levelTextureRegions.add(BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mLevelTexture1,
				gameLogicController, "level3.png", 0, 128));
		levelTextureRegions.add(BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mLevelTexture1,
				gameLogicController, "level_4.png", 0, 192));
		levelTextureRegions.add(BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mLevelTexture1,
				gameLogicController, "level_5.png", 0, 256));
		levelTextureRegions.add(BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mLevelTexture1,
				gameLogicController, "level_6.png", 0, 320));
		levelTextureRegions.add(BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mLevelTexture1,
				gameLogicController, "level_7.png", 0, 384));
		levelTextureRegions.add(BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mLevelTexture1,
				gameLogicController, "level_8.png", 0, 448));
		levelTextureRegions.add(BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mLevelTexture1,
				gameLogicController, "level_9.png", 0, 512));
		levelTextureRegions.add(BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mLevelTexture1,
				gameLogicController, "level_10.png", 0, 576));
		mLevelTextureRegionLocked = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mLevelTexture1,
				gameLogicController, "level_locked_1.png", 0, 640);


		gameLogicController
				.getEngine()
				.getTextureManager()
				.loadTextures(this.mLevelButtonDisabled, this.mLevelChooseBackTexture,
						this.mLevelChooseMenuTexture, this.mLevelChooseNextTexture,
						this.mLevelTexture1);
	}

	public void createLevelMenuButtonLayout(Scene scene, boolean backButton) {
		int height = 150;
		int weight = 50;
		int x = 200;
		int y = 200;
		Sprite buttonLevel = new Sprite(x, y, height, weight, mLevelChooseBackTextureRegion) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (gameLogicController.checkTouchTime())
					gameLogicController.getEngine().setScene(gameLogicController.newGameLevelScene(1));
				return true;
			}
		};
		scene.registerTouchArea(buttonLevel);
		scene.attachChild(buttonLevel);
	}

	public Scene createLevelSubmenu() {
		Scene scene = new Scene();
		addMenuLevelItems(scene, currentPage);
		return scene;
	}

	/*
	 * LevelId = 100 - 130
	 */
	// set up all menu level items
	public void addMenuLevelItems(Scene scene, int pageNumber) {
		int height = 80;
		int weight = 80;
		int x = 10;
		int y = 10;

		for (int i = 0; i < 10; i++) {
			addLevelButton(scene, x, y, height, weight, i + 1, levelTextureRegions.get(i));
			x += weight + 10;
			// new line
			if (i == 4) {
				x = 10;
				y += height + 10;
			}
		}

		y += height + 10;
		x = 20;
		height = 120;
		weight = 120;
		
		switch (pageNumber) {
		case 1:
			addControlButtonsPage1(scene, x, y, height, weight);
			break;
		case 2:
			addControlButtonsPage2(scene, x, y, height, weight);
			break;
		}
	}

	private void addLevelButton(Scene scene, int x, int y, int height, int weight, final int levelId,
			final TextureRegion levelTextureRegion) {
		Sprite buttonLevel;
		if (gameLogicController.getPlayerProfileManager().isLevelUnlocked(levelId)) {
			buttonLevel = new Sprite(x, y, height, weight, levelTextureRegion) {
				@Override
				public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
					if (gameLogicController.checkTouchTime()) {
						gameLogicController.getEngine().setScene(gameLogicController.newGameLevelScene(levelId));
					}
					return true;
				}
			};
			scene.registerTouchArea(buttonLevel);
			scene.attachChild(buttonLevel);
		} else {
			buttonLevel = new Sprite(x, y, height, weight, mLevelTextureRegionLocked);
			scene.attachChild(buttonLevel);
		}
	}

	private void addControlButtonsPage1(Scene scene, int x, int y, int height, int weight) {
		// BUTTONS
		Sprite buttonLevel = new Sprite(x, y, height, weight, mLevelButtonBackDisabledRegion);
		scene.attachChild(buttonLevel);
		x += weight + 30;

		buttonLevel = new Sprite(x, y, height, weight, mLevelChooseMenuTextureRegion) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (gameLogicController.checkTouchTime())
					gameLogicController.getEngine().setScene(gameLogicController.getMainMenu().createMenuScene());
				return true;
			}
		};
		scene.registerTouchArea(buttonLevel);
		scene.attachChild(buttonLevel);
		x += weight + 30;

		buttonLevel = new Sprite(x, y, height, weight, mLevelChooseNextTextureRegion) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (gameLogicController.checkTouchTime()) {
					currentPage += 1;
					gameLogicController.getEngine().setScene(createLevelSubmenu());
				}
				return true;
			}
		};
		scene.registerTouchArea(buttonLevel);
		scene.attachChild(buttonLevel);
	}

	public void addControlButtonsPage2(Scene scene, int x, int y, int height, int weight) {
		Sprite buttonLevel = new Sprite(x, y, height, weight, mLevelChooseBackTextureRegion) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (gameLogicController.checkTouchTime()) {
					currentPage -= 1;
					gameLogicController.getEngine().setScene(createLevelSubmenu());
				}
				return true;
			}
		};
		scene.registerTouchArea(buttonLevel);
		scene.attachChild(buttonLevel);
		x += weight + 30;

		buttonLevel = new Sprite(x, y, height, weight, mLevelChooseMenuTextureRegion) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (gameLogicController.checkTouchTime())
					gameLogicController.getEngine().setScene(gameLogicController.getMainMenu().createMenuScene());
				return true;
			}
		};
		scene.registerTouchArea(buttonLevel);
		scene.attachChild(buttonLevel);
		x += weight + 30;

		buttonLevel = new Sprite(x, y, height, weight, mLevelButtonNextDisabledRegion);
		scene.attachChild(buttonLevel);

	}

}
